(function(){
    var proposal;
    var tm;
    /**
	* 初始化
	*/
    async function init(){
        var rtn = await loadManbers();
        if (rtn){
            proposal = getNode("detail.js").tag;
            setNodeText("proposalContent",proposal.body.replaceAll("\r\n","<br>"));
            //匿名投票就显示投票人，非匿名就显示三个列表
            if (proposal.isAnonymous){
                setNodeText("voterCount",proposal.voters.length);
            }else{
                setList("approvalList",proposal.approvalVoters);
                setList("refuseList",proposal.refuseVoters);
                setList("waiverList",proposal.voters);
            }
            //显示反对理由
            var list = getNode("reasonList");
            clearAll(list);
            for(var i=0;i<proposal.reason.length;i++){
                var div = newNode("div");
                div.innerText=proposal.reason[i];
                list.appendChild(div);
            }
            //看看是否投过票了
            var voted = false;
            var tmp = [];
            proposal.approvalVoters.forEach(item => {tmp.push(item)});
            proposal.refuseVoters.forEach(item => {tmp.push(item)});
            proposal.voters.forEach(item => {tmp.push(item)});
            for(var i=0;i<tmp.length;i++){
                if (tmp[i] == publicParam.address) {
                    voted=true;
                    break;
                }
            }
            //根据是否重大决定等待日期
            var s = 1000*60*60*24*(proposal.isBag?2:1);
            //计算投票开始时间
            var start = new Date(proposal.proposalTime*1000 + s);
            function showTimer(){
                var wating = start > new Date().getTime();
                if (wating){
                    //总秒数
                    var t = Math.floor((start - new Date().getTime())/1000);
                    //小时
                    var h = Math.floor(t/(3600));
                    //分
                    var m = Math.floor((t-h*3600)/(60));
                    //秒
                    var s = t - h*3600 - m*60;
                    setNodeText("timer","距离投票开始还有"+h+"小时"+m+"分"+s+"秒");
                    display("buttons1",false);
                    display("reason",false);
                    tm = setTimeout(showTimer,1000);
                }else{
                    var my = getMy();
                    setNodeText("timer","");
                    //非正式会员或已经投过票或投票已经结束，隐藏投票按钮
                    if (voted || proposal.votingPassed || !my || !my.isReal) {
                        display("buttons1",false);
                        display("reason",false);
                    }else{
                        display("buttons1",true);
                        display("reason",true);
                        getNode("approval").onclick=approval;
                        getNode("refuse").onclick=refuse;
                        getNode("waiver").onclick=waiver;
                    }
                }
            };
            showTimer();
            //只有管理员才能看到结束和计票按钮
            if (publicParam.isProposaAdmin){
                if (proposal.paid) display("buttons2",false);
                else {
                    display("buttons2",true);
                    getNode("finish").onclick=finish;
                }
            }else display("buttons2",false);
            //切换显示的内容
            display("realName",!proposal.isAnonymous);
            display("anonymous",proposal.isAnonymous);
            //返回按钮
            getNode("goback").onclick=function(){
                clearTimeout(tm);
                goto("proposal/list");
            };
        }
    }
    /**
    * 通过ID设置节点是否可见，覆盖了main中的display，因为如果设为block会导致按钮换行
    */
    function display(name,b){
        var div = getNode(name);
        if (b) div.style.display="";
        else div.style.display="none";
    }  
    /**
	* 实际的投票操作
	*/
    async function vote(decision){
        var reason = getNode("reason").value;
        if (decision==1) reason="";
        else if(reason==""){
            alert("请输入理由");
            return;
        }
        setLoading(1);
        const rtn = await proposalVote("投票",proposal.id,decision,reason);
        if(rtn){
            //更新显示
            if (proposal.isAnonymous){
                arrPush(proposal.voters,publicParam.address);
                setNodeText("voterCount",proposal.voters.length);
            }else{
                if (decision==1) {
                    addLine("approvalList",publicParam.address);
                    arrPush(proposal.approvalVoters,publicParam.address);
                }else if(decision==2) {
                    addLine("refuseList",publicParam.address);
                    arrPush(proposal.refuseVoters,publicParam.address);
                }else {
                    addLine("waiverList",publicParam.address);
                    arrPush(proposal.voters,publicParam.address);
                }
            }
            if (reason!=""){
                var list = getNode("reasonList");
                var div = newNode("div");
                div.innerText=reason;
                list.appendChild(div);
                arrPush(proposal.reason,reason);
            }
            display("reason",false);
            display("buttons1",false);
        }
        setLoading(0);
    }
    /**
	* 不知道为什么直接往这个数组中Push会出错，而先复制出来，再贴回去就可以
	*/
    function arrPush(arr,value){
        var tmp = [];
        arr.forEach(item => {tmp.push(item)});
        tmp.push(value);
        arr = tmp;
    }
    /**
	* 赞成票
	*/
    function approval(){
        vote(1);
    }
    
    /**
	* 反对票
	*/
    function refuse(){
        vote(2);
    }
    
    /**
	* 弃权票
	*/
    function waiver(){
        vote(3);
    }
    
    /**
	* 设置投票人列表，每人都可以点击跳转
	*/
    function setList(name,data){
        var list = getNode(name);
        if (data.length==0){
            list.style.display="none";
        }else{
            var line = list.children[0];
            clearAll(list);
            for(var i=0;i<data.length;i++){
                var newLine = line.cloneNode(true);
                addListItem(newLine,data[i]);
                list.appendChild(newLine);
            }
        }
    }

    /**
	* 增加一个投票人
	*/
    function addLine(name,address){
        var list = getNode(name);
        list.style.display="block";
        var newLine = list.children[0].cloneNode(true);
        addListItem(newLine,address);
        list.appendChild(newLine);
    }

    /**
	* 增加一个投票人项目
	*/
    function addListItem(newLine,address){
        var m = getMemberByAddress(address);
        newLine.children[0].innerText = m.name;
        newLine.children[0].tag = m.address;
        newLine.children[0].onclick=function(){
            goto("member/info",this.tag);
        }
    }

    /**
	* 结束投票
	*/
    async function finish(){
        setLoading(1);
        const rtn = await voteCounting("结束投票、进行计票",proposal.id);
        if (rtn){
            proposal.votingPassed = true;
            proposal.paid = true;
            display("buttons1",false);
            display("buttons2",false);
        }
        setLoading(0);
    }
    init();
})();